//
//  ANOptionFootView.m
//  Ants
//
//  Created by ZHM on 16/7/2.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANOptionFootView.h"

#define INPUT_LENGTH 15  //限制输入长度
#define FootView_height 35


@interface ANOptionFootView ()<UITextFieldDelegate>
@property (nonatomic, weak) UIButton *addCategory;

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, assign) BOOL inputStateIs;
@property (nonatomic, assign) NSInteger lastLength;

@end


@implementation ANOptionFootView

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 200, self.height)];
        _textField.placeholder = @"输入..";
        _textField.alpha = 0;
        _textField.returnKeyType = UIReturnKeyJoin;
        _textField.delegate = self;
        [self addSubview:_textField];
        [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textField;
}
+ (instancetype)footView {
    ANOptionFootView *footView = [[ANOptionFootView alloc] initWithFrame:CGRectMake(0, 0, ANKeyWindow.width, FootView_height)];
    footView.backgroundColor = [UIColor colorWithRed:246.0/255 green:246.0/255 blue:249.0/255 alpha:1];
    return  footView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addUpAllChildView];
        self.userInteractionEnabled = YES;
    }
    return self;
}
- (void)addUpAllChildView {
    UIButton *addCategory = [UIButton buttonWithType:UIButtonTypeCustom];
    [addCategory setTitle:@"添加新类" forState:UIControlStateNormal];
    [addCategory setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [addCategory addTarget:self action:@selector(clickAddNewCategory) forControlEvents:UIControlEventTouchUpInside];
    addCategory.titleLabel.font = [UIFont systemFontOfSize:ANFindToolFontSize];
    [addCategory setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _addCategory = addCategory;
    [self addSubview:_addCategory];
    _addCategory.frame = CGRectMake((ANKeyWindow.width - 100)/2, 0, 100, FootView_height);
}
- (void)layoutSubviews {
    [super layoutSubviews];
    //设置按钮frame ----要动画不要在这里设置Frame
}
- (void)clickAddNewCategory {
    if (!_addCategory.isSelected) {
        [self showInputView];
    }else {
        //添加类
        [self AddNewCategory];
    }
}
- (void)AddNewCategory {
    ANLog(@"_textField:%@",_textField.text);
    if (_textField.text.length == 0) {
        return;
    }else {
        //判断是否已经存在
    }
    [sharedWebService addCategory:_textField.text completion:^(ANBDProductCategory *productCategory,BOOL succeeded, NSError *error) {
        if (succeeded) {
            ANLog(@"添加成功");
            [SVProgressHUD showSuccessWithStatus:@"添加成功!"];
            if ([self.delegate respondsToSelector:@selector(updateTableViewWithNewCategory:)]) {
                [self.delegate updateTableViewWithNewCategory:productCategory];
            }
            //添加成功
            [self hideInputViewAnimation:YES];
        } else {
            ANLog(@"添加失败:%@",error);
            [SVProgressHUD showSuccessWithStatus:@"添加失败!"];
        }
    }];
}
- (void)textFieldDidChange:(UITextField *)textField {
    //限制输入长度
    if (textField.text.length > INPUT_LENGTH) {
        textField.text = [textField.text substringToIndex:_lastLength];
    }
    _lastLength = textField.text.length;
}

#pragma mark --键盘返回键
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == _textField) {
        [self clickAddNewCategory];
    }
    return YES;
}

- (void)showInputView {
    [UIView animateWithDuration:ANFindToolAnimationTime animations:^{
        _addCategory.transform = CGAffineTransformMakeTranslation((ANKeyWindow.width - 100)/2, 0);
        self.textField.alpha = 1;
    } completion:^(BOOL finished) {
        _addCategory.selected = YES;
    }];
}
- (void)hideInputViewAnimation:(BOOL)animation {
    if (animation) {
        [UIView animateWithDuration:ANFindToolAnimationTime animations:^{
            _addCategory.transform =  CGAffineTransformIdentity;
            _textField.alpha = 0;
        } completion:^(BOOL finished) {
            _addCategory.selected = NO;
            [_textField removeFromSuperview];
            _textField = nil;
        }];
    } else {
        _addCategory.transform =  CGAffineTransformIdentity;
        _textField.alpha = 0;
        _addCategory.selected = NO;
        [_textField removeFromSuperview];
        _textField = nil;
    }
}

- (void)hideInputView {
    _addCategory.transform =  CGAffineTransformIdentity;
    _textField.alpha = 0;
    _addCategory.selected = NO;
    [_textField removeFromSuperview];
    _textField = nil;
}


@end
